//
//  TabTitleView.m
//  兼职页面
//
//  Created by kuiyuan liang on 2021/5/23.
//

#import "TabTitleView.h"
#import "CommonUtil.h"

@interface TabTitleView()

@property (strong, nonatomic) UIButton * selectBtn;
@property (strong, nonatomic) UIView * lineView;
@property (strong, nonatomic) UIView * indexView;
@end


@implementation TabTitleView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        CGFloat w = self.width/self.titles.count;
        self.backgroundColor = [UIColor whiteColor];
        for (NSInteger i = 0; i < self.titles.count; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(w * i, 0, w, self.height);
            [btn setTitle:self.titles[i] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [btn setTitleColor:RGB(255.0, 101.0, 125.0) forState:UIControlStateSelected];
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            btn.tag = i;
            [self addSubview:btn];
            if (i == 0) {
                self.selectBtn = btn;
            }
        }
        [self addSubview:self.lineView];
        [self addSubview:self.indexView];
        [self btnClick:self.selectBtn];
    }
    return self;
}

-(void)btnClick:(UIButton *)sender{
    self.selectBtn.selected = false;
    self.selectBtn = sender;
    self.selectBtn.selected = YES;
    [UIView animateWithDuration:0.25 animations:^{
        self.indexView.centerX = sender.centerX;
    }];
    if (self.actionBlock) {
        self.actionBlock(sender.tag);
    }
}

#pragma mark - lazy

-(NSArray *)titles{
    return  @[@"综合",@"关注",@"精华",@"最多赞",@"最新",@"规则"];
}

-(UIView *)lineView{
    if (!_lineView) {
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height-AD(0.5), self.width, AD(0.5))];
        _lineView.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.3];
    }
    return _lineView;
}

-(UIView *)indexView{
    if (!_indexView) {
        _indexView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height-AD(1), AD(35), AD(1))];
        _indexView.backgroundColor = RGB(255.0, 101.0, 125.0);
    }
    return _indexView;
}


@end
